package org.example.javafxdemo;

/**
 * @Author 56465
 * @Create 2024/7/29 17:54
 */
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import org.commonmark.ext.gfm.tables.TablesExtension;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;

import java.util.Arrays;

public class MarkdownPager2 extends Application {
    @Override
    public void start(Stage primaryStage) {
        TextArea markdownInput = new TextArea();
        markdownInput.setPromptText("Enter Markdown here...");

        WebView htmlOutput = new WebView();

        Button convertButton = new Button("Convert to HTML");
        convertButton.setOnAction(e -> {
            String markdownText = markdownInput.getText();
            Parser parser = Parser.builder().build();
            Node document = parser.parse(markdownText);
            HtmlRenderer renderer = HtmlRenderer.builder()
                    .extensions(Arrays.asList(TablesExtension.create()))
                    .build();
            String html = renderer.render(document);
            htmlOutput.getEngine().loadContent(html);
        });

        VBox leftPane = new VBox(markdownInput, convertButton);
        leftPane.setPadding(new javafx.geometry.Insets(10));

        BorderPane root = new BorderPane();
        root.setLeft(leftPane);
        root.setRight(htmlOutput);

        Scene scene = new Scene(root, 800, 600);

        primaryStage.setTitle("Markdown Editor");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}